package com.xf.leecode.linked;

/**
 * @author zouxf Department:3cs
 * @date 2023/2/9 9:12
 * @description
 * @see
 */
public class LinkedData<E> {
    private Node<E> first;
    private Node<E> last;

    public void add(E e) {
        Node<E> l = last;
        Node<E> newNode = new Node<>(e, l, null);
        last = newNode;
        if (l == null) {
            first = newNode;
        } else {
            l.next = newNode;
        }
    }

    @Override
    public String toString() {
        return "LinkedData{" +
                "first=" + first +
                ", last=" + last +
                '}';
    }

    static class Node<E>{
        E item;
        Node<E> prev;
        Node<E> next;

        public Node(E item, Node<E> prev, Node<E> next) {
            this.item = item;
            this.prev = prev;
            this.next = next;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "item=" + item +
                    '}';
        }
    }

    public static void main(String[] args) {
        LinkedData<String> linkedData = new LinkedData<>();
        linkedData.add("xiaoB");
        linkedData.add("xiaoA");
        linkedData.add("xiaoC");
        System.out.println(linkedData);
    }

}
